package com.example.project.common;

import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.multipart.support.MissingServletRequestPartException;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(MethodArgumentNotValidException.class)
    public Result<?> handleValidationException(MethodArgumentNotValidException e) {
        String message = e.getBindingResult().getFieldError().getDefaultMessage();
        log.error("参数验证失败：{}", message);
        return Result.error(message);
    }

    @ExceptionHandler(MissingServletRequestPartException.class)
    public Result<?> handleMissingPart(MissingServletRequestPartException e) {
        log.error("文件上传错误: {}", e.getMessage());
        return Result.error("请选择要上传的文件");
    }

    @ExceptionHandler(Exception.class)
    public Result<?> handleException(Exception e) {
        log.error("系统错误", e);
        return Result.error("系统错误，请稍后重试");
    }
} 